package com.example.yangqing.weexlearndemoandroid;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.taobao.weex.IWXRenderListener;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.common.WXRenderStrategy;
import com.taobao.weex.utils.WXFileUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by DELL on 2018/1/3.
 */

public class BaseWeexActivity extends AppCompatActivity implements IWXRenderListener{
    WXSDKInstance mWXSDKInstance;
    private ShakeListener shakeListener;

    public boolean useNet = true;//是从网络还是本地加载js  默认网络

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        mWXSDKInstance = new WXSDKInstance(this);
        mWXSDKInstance.registerRenderListener(this);
        String url = "http://192.168.1.105:8080/dist/index.js";
        render(url);
        shakeListener = new ShakeListener(this);
        shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
            @Override
            public void onShake() {
                String bundleUrl = mWXSDKInstance.getBundleUrl();//获取当前加载的地址
                mWXSDKInstance.destroy();
                mWXSDKInstance = null;
                mWXSDKInstance = new WXSDKInstance(BaseWeexActivity.this);
                mWXSDKInstance.registerRenderListener(BaseWeexActivity.this);
                Toast.makeText(BaseWeexActivity.this, "重新加载\n"+bundleUrl, Toast.LENGTH_SHORT).show();
                render(bundleUrl);
            }
        });

    }

    private void render(String url) {
        final Map options = new HashMap();
        options.put("baseUrl","http://www.baidu.com");
        if(useNet){
            mWXSDKInstance.renderByUrl(BuildConfig.APPLICATION_ID,url,options,null, WXRenderStrategy.APPEND_ASYNC);
        }else {
            mWXSDKInstance.render(BuildConfig.APPLICATION_ID, WXFileUtils.loadAsset("hello.js",BaseWeexActivity.this),
                    options,null,WXRenderStrategy.APPEND_ASYNC);
        }
    }

    @Override
    public void onViewCreated(WXSDKInstance instance, View view) {
        setContentView(view);
    }
    @Override
    public void onRenderSuccess(WXSDKInstance instance, int width, int height) {
    }
    @Override
    public void onRefreshSuccess(WXSDKInstance instance, int width, int height) {
    }
    @Override
    public void onException(WXSDKInstance instance, String errCode, String msg) {
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(mWXSDKInstance!=null){
            mWXSDKInstance.onActivityResume();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        if(mWXSDKInstance!=null){
            mWXSDKInstance.onActivityPause();
        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        if(mWXSDKInstance!=null){
            mWXSDKInstance.onActivityStop();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mWXSDKInstance!=null){
            mWXSDKInstance.onActivityDestroy();
        }
    }

}
